package com.jsz.peini.common.util.wxpay;

import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsz.peini.common.push.json.JSONObject;
import com.jsz.peini.controller.test.TestController;

/**
 * 模拟  http 请求
 * @author Administrator
 *
 */
public class GetWxOrderno {
	private static final Logger logger = LoggerFactory.getLogger(GetWxOrderno.class);
	public static DefaultHttpClient httpclient;

	static {
		// PoolingClientConnectionManager 连接池
		httpclient = new DefaultHttpClient(new PoolingClientConnectionManager());
		httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);
	}

	public static String getPayNo(String url, String xmlParam) {
		DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager());
		client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		HttpPost httpost = HttpClientConnectionManager.getPostMethod(url);
		JSONObject object = new JSONObject();
		String prepay_id = "";
		try {
			httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			HttpResponse response = httpclient.execute(httpost);
			HttpEntity responseEntity = response.getEntity();
			String jsonStr = EntityUtils.toString(responseEntity, "UTF-8");
			logger.info(jsonStr);
			//释放 链接
			/*if (httpost != null) {
				httpost.abort();
			}*/
			
			Map map = XMLUtil.doXMLParse(jsonStr);
			String return_code = (String) map.get("return_code");
			if (return_code.equals("FAIL")) {
				object.put("code", "0");
				object.put("msg", (String) map.get("return_msg"));
				return object.toString();
			}else{
				String result_code = (String) map.get("result_code");
				if(result_code.equals("FAIL")){					
					object.put("code", "0");
					object.put("msg", (String) map.get("err_code_des"));
					return object.toString();
				}else{
					object.put("prepay_id", (String) map.get("prepay_id"));
					if(map.get("trade_type").toString().equals("NATIVE")){
						object.put("code_url", (String) map.get("code_url"));
					}
					object.put("code", "1");
					object.put("msg", "");
				}
			}
			
			//prepay_id = (String) map.get("prepay_id");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}
	
	public static String queryOrder(String url, String xmlParam) {
		DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager());
		client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		HttpPost httpost = HttpClientConnectionManager.getPostMethod(url);
		JSONObject object = new JSONObject();
		String prepay_id = "";
		try {
			httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			HttpResponse response = httpclient.execute(httpost);
			HttpEntity responseEntity = response.getEntity();
			String jsonStr = EntityUtils.toString(responseEntity, "UTF-8");
			//释放 链接
			/*if (httpost != null) {
				httpost.abort();
			}*/
			
			Map map = XMLUtil.doXMLParse(jsonStr);
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}

}